import { createApp, defineComponent, h } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import { createI18n } from 'vue-i18n'
import locales from './locales/index'
import formCreate from '@form-create/arco-design'
import install from '@form-create/arco-design/auto-import'
import { createPinia } from 'pinia'
import { useConfigStore } from './store/config'
import Editor from './components/Editor.vue'
import DiffEditor from './components/DiffEditor.vue'
const i18n = createI18n({
  locale: 'zh',
  fallbackLocale: 'en',
  messages: locales,
})

const divComponent = defineComponent({
  name: 'div',
  setup() {
    return () => h('div');
  }
});
const iframeComponent = defineComponent({
  name: 'iframe',
  setup() {
    return () => h('iframe', { frameborder: 0, scrolling: "no" });
  }
});
formCreate.use(install)
formCreate.component('div', divComponent)
formCreate.component('iframe', iframeComponent)
formCreate.component('editor', Editor)
formCreate.component('diff-editor', DiffEditor)

const pinia = createPinia()

createApp(App).use(i18n).use(router).use(formCreate).use(pinia).mount('#app').$nextTick(() => {
  // Use contextBridge
  window.ipcRenderer.invoke('initialize').then((res) => {
    const store = useConfigStore()
    store.set(res.config)
    let locales = res.locales || {}
    for (let k in locales) {
      i18n.global.mergeLocaleMessage(k, locales[k])
    }
    i18n.global.locale = res.config.locale
  })
})
